package com.gears_software.sxdc.dccx.ui.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.MyTripZCListAdapter;
import com.gears_software.sxdc.dccx.data.finder.IZPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MyTripZCModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RDeleteZporderModel;
import com.gears_software.sxdc.dccx.ui.activity.MessageCenterActivity;
import com.gears_software.sxdc.dccx.ui.activity.MyTripZCListDetailActivity_;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/8/16.
 */

@EFragment(R.layout.fragment_mytrip_specialcar)
public class MyTripSpecialCarFragment extends Fragment {
    @App
    DCCXApplication app;
    @ViewById
    ListView list_zc_trip;
    @ViewById
    RelativeLayout re_wu;
    @ViewById
    TextView txt_nodata_icon;
    @RestService
    IZPOrderFinder izpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    String type = "2";  //专车乘客端
    List<MyTripZCModel> zcModelList;
    MyTripZCListAdapter adapter;
    MaterialDialogProgress progress;
    int MENU_DEL = 1;
    int positon;


    @AfterViews
    void created() {
        initIconFont();
        izpOrderFinder.setRestErrorHandler(errorHandler);
        zcModelList = new ArrayList<MyTripZCModel>();
        progress = new MaterialDialogProgress(getActivity());
        progress.show("正在加载...");
        getZCTripList();
    }

    void initIconFont() {
        txt_nodata_icon.setTypeface(app.iconfont);
    }

    @Background
    void getZCTripList() {
        ResultModel<List<MyTripZCModel>> res = izpOrderFinder.getZCTripList(type);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<List<MyTripZCModel>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    zcModelList = res.getData();
                    adapter = new MyTripZCListAdapter(zcModelList, getActivity());
                    list_zc_trip.setAdapter(adapter);
                    if (zcModelList != null && zcModelList.size() > 0) {
                        re_wu.setVisibility(View.GONE);
                        list_zc_trip.setVisibility(View.VISIBLE);
                        registerForContextMenu(list_zc_trip);
                    } else {
                        re_wu.setVisibility(View.VISIBLE);
                        list_zc_trip.setVisibility(View.GONE);
                    }
                }
            } else {
                TastyToast.makeText(getActivity(), res.getErrmsg(), app.SHORT, app.ERROR);
            }
        }
        progress.dismiss();

    }

    @ItemClick
    void list_zc_trip(MyTripZCModel zcModel) {
        if (zcModel.getDriverid() != null) {
            Intent intent = new Intent(getActivity(), MyTripZCListDetailActivity_.class);
            intent.putExtra("id", zcModel.getId());
            startActivity(intent);
        } else {
            TastyToast.makeText(getActivity(), "此订单没有详细信息。", app.SHORT, app.WARNING);
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MENU_DEL, 0, "删除记录");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //获取上下文菜单适配器
        AdapterView.AdapterContextMenuInfo cmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        //获取被选择的菜单位置
        positon = cmi.position;

        if (item.getItemId() == MENU_DEL) {
            new MaterialDialog.Builder(getActivity())
                    .title("提示")
                    .titleGravity(GravityEnum.CENTER)
                    .titleColorRes(R.color.colorFontGray)
                    .content("确定删除选中信息吗？")
                    .contentColorRes(R.color.black)
                    .positiveText("确定")
                    .positiveColor(ContextCompat.getColor(getActivity(), R.color.colorFontBlue))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            RDeleteZporderModel deleteZporderModel = new RDeleteZporderModel();
                            deleteZporderModel.setOrderid(zcModelList.get(positon).getId());
                            deleteOrder(deleteZporderModel);
                        }
                    })
                    .negativeText("取消")
                    .negativeColor(ContextCompat.getColor(getActivity(), R.color.colorFontGray))
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    })
                    .buttonsGravity(GravityEnum.CENTER)
                    .show();

        }
        return super.onContextItemSelected(item);
    }

    @Background
    void deleteOrder(RDeleteZporderModel deleteZporderModel) {
        ResultModel<String> res = izpOrderFinder.deleteZporder(deleteZporderModel);
        showDeleteResult(res);
    }

    @UiThread
    void showDeleteResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                zcModelList.remove(positon);
                adapter.notifyDataSetChanged();
                TastyToast.makeText(getActivity(), res.getData(), app.SHORT, app.SUCCESS);
            } else {
                TastyToast.makeText(getActivity(), res.getErrmsg(), app.SHORT, app.ERROR);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//        progress.show("正在加载...");
        getZCTripList();
    }
}
